package org.linotte.dll;

import java.util.List;

import org.linotte.greffons.externe.Greffon;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;

/**
* http://jnative.sourceforge.net/docs/org/xvolks/jnative/JNative.html
*
*/

public class Adaptateur extends Greffon {

	@Slot(nom = "appelerdll")
	public String appelerDLL(String DLL, String fonction, String typeRetour, List<String> types, List<String> parametres) throws GreffonException {
		String retour;
		try {
			JNative dllAAppeler = new JNative(DLL, fonction);
			dllAAppeler.setRetVal(getType(typeRetour));
			int pos = 0;
			for (String string : parametres) {
				String sType = types.get(pos);
				Type type = getType(sType);
				dllAAppeler.setParameter(pos, type, string);
				pos++;
			}
			dllAAppeler.invoke();
			retour = dllAAppeler.getRetVal();
		} catch (Throwable e) {
			e.printStackTrace();
			throw new GreffonException("Erreur dans le greffon Native : " + e.getMessage());
		}
		return retour;
	}

	private Type getType(String sType) {
		Type type = Type.STRING;
		if ("entier".equals(sType))
			type = Type.INT;
		if ("décimal".equals(sType))
			type = Type.DOUBLE;
		return type;
	}

}